package com.huike.web.controller.report;


import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.SecurityUtils;
import com.huike.report.domain.vo.IndexTodayInfoVO;
import com.huike.report.domain.vo.IndexTodoInfoVO;
import com.huike.report.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IReportService reportService;


    /**
     * 首页--基础数据统计
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/getBaseInfo")
    public AjaxResult getBaseInfo(@RequestParam("beginCreateTime") String beginCreateTime,
                                  @RequestParam("endCreateTime") String endCreateTime) {
        return AjaxResult.success(reportService.getBaseInfo(beginCreateTime, endCreateTime));
    }

    /**
     * 首页--商机转化龙虎榜
     *
     * @param request
     * @return
     */
    @GetMapping("/businessChangeStatistics")
    public AjaxResult businessChangeStatistics(IndexStatisticsVo request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        request.setBeginCreateTime(request.getBeginCreateTime() + " 00:00:00");
        request.setEndCreateTime(request.getEndCreateTime() + " 23:59:59");
        List<Map<String, Object>> list = reportService.businessChangeStatisticsForIndex(request);
        return AjaxResult.success(list);
    }

    /**
     * 首页--线索转化龙虎榜
     *
     * @param request
     * @return
     */
    @GetMapping("/salesStatistic")
    public AjaxResult salesStatistics(IndexStatisticsVo request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        request.setBeginCreateTime(request.getBeginCreateTime() + " 00:00:00");
        request.setEndCreateTime(request.getEndCreateTime() + " 23:59:59");
        List<Map<String, Object>> list = reportService.clueChangeStatisticsForIndex(request);
        return AjaxResult.success(list);
    }

    /*获取首页今日简报数据如下:
	今日线索数量：当日分配给当前用户的线索数量
	今日商机数量：当日分配给当前用户的商机数量
	今日合同数量：当前用户当日创建的合同
	今日成交金额：当前用户当日成交的合同金额*/
    @GetMapping("/getTodayInfo")
    public AjaxResult getTodayInfo() {
//        准备当前日期
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        准备当前用户
        String username = SecurityUtils.getUsername();
//调用service层
        IndexTodayInfoVO todayInfoVO = reportService.getTodayInfo(today, username);
//        3.返回结果
        return AjaxResult.success(todayInfoVO);
    }

    //    首页待办,待跟进待分配的线索商机
    @GetMapping("/getTodoInfo")
    public AjaxResult getTodoInfo(@RequestParam("beginCreateTime") String beginCreateTime,
               @RequestParam("endCreateTime")String endCreateTime){//1.获取请求参数
//        2.调用service层方法
        IndexTodoInfoVO indexTodoInfoVO=reportService.getTodoInfo(beginCreateTime,endCreateTime);
//        3.返回结果
        return AjaxResult.success(indexTodoInfoVO);
    }
}